package ua.pp.bizon.yandex.nb.model;

import java.io.File;
import java.io.FileFilter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Enumeration;
import java.util.List;

import javax.swing.tree.TreeNode;

public class LocalNode implements TreeNode {

	private final class FileFilterImplementation implements FileFilter {
		@Override
		public boolean accept(File pathname) {
			return pathname.isDirectory();
		}
	}

	private File path;
	private LocalNode parent;
	private List<LocalNode> childrens;
	
	public LocalNode(String s) {
		this.path = new File(s);
	}
	
	private LocalNode(File s, LocalNode parent) {
		this.path = s;
		this.parent = parent;
	}
	public List<LocalNode> getChildrens() {
		if (childrens == null){
			childrens = new ArrayList<LocalNode>();
			for (File f: path.listFiles(new FileFilterImplementation())){
				childrens.add(new LocalNode(f, this));
			}
		}
		return childrens;
	}

	@Override
	public TreeNode getChildAt(int childIndex) {
		return getChildrens().get(childIndex);
	}

	@Override
	public int getChildCount() {
		return getChildrens().size();
	}

	@Override
	public TreeNode getParent() {
		return parent;
	}

	@Override
	public int getIndex(TreeNode node) {
		return getChildrens().indexOf(node);
	}

	@Override
	public boolean getAllowsChildren() {
		return path.isDirectory();
	}

	@Override
	public boolean isLeaf() {
		return path.isFile();
	}

	@Override
	public Enumeration<LocalNode> children() {
		return Collections.enumeration(getChildrens());
	}

	@Override
	public String toString() {
		return path.getName();
	}

	public File getFile() {
		return path;
	}
}
